package com.huike.clues.MpService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.MpMapper.MpSysDictTypeMapper;
import com.huike.clues.MpService.MpSysDictTypeService;
import com.huike.clues.domain.dto.MPSysDictTypeDTO;
import com.huike.common.core.domain.model.LoginUser;
import com.huike.common.core.page.PageDomain;
import com.huike.common.core.page.TableSupport;
import com.huike.common.entity.MPSysDictType;
import com.huike.common.getUserId.TokenService;
import com.huike.common.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MpSysDictTypeServiceImpl implements MpSysDictTypeService {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MpSysDictTypeMapper mpSysDictTypeMapper;
    @Override
    public void addType(MPSysDictTypeDTO mpSysDictTypeDTO) {
        LoginUser loginUser = tokenService.getLoginUser(request);

        MPSysDictType mpSysDictType = new MPSysDictType();
        BeanUtils.copyProperties(mpSysDictTypeDTO,mpSysDictType);
        mpSysDictType.setCreateBy(loginUser.getUsername());
        mpSysDictType.setCreateTime(LocalDateTime.now());
        mpSysDictType.setUpdateBy(loginUser.getUsername());
        mpSysDictType.setUpdateTime(LocalDateTime.now());
        mpSysDictTypeMapper.insert(mpSysDictType);
    }

    /**
     * 修改字典类型
     * @param mpSysDictTypeDTO
     */
    @Override
    public void updateType(MPSysDictType mpSysDictTypeDTO) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        MPSysDictType mpSysDictType = new MPSysDictType();
        BeanUtils.copyProperties(mpSysDictTypeDTO,mpSysDictType);
        mpSysDictType.setUpdateBy(loginUser.getUsername());
        mpSysDictType.setUpdateTime(LocalDateTime.now());
        mpSysDictTypeMapper.updateById(mpSysDictType);
    }

    /**
     * 获取字典选择框列表
     * @return
     */
    @Override
    public List<MPSysDictType> optionselect() {
        List<MPSysDictType> mpSysDictTypes = mpSysDictTypeMapper.selectList(null);
        return mpSysDictTypes;
    }

    /**
     * 删除字典类型
     * @param dictIds
     */
    @Override
    public void deleteType(List<Long> dictIds) {
        mpSysDictTypeMapper.deleteBatchIds(dictIds);
    }

    /**
     * 查询字典类型详细
     * @param dictId
     * @return
     */
    @Override
    public MPSysDictType findType(Long dictId) {
        MPSysDictType mpSysDictType = mpSysDictTypeMapper.selectById(dictId);
        return mpSysDictType;
    }

    /**
     * 分页查询数据字典类型信息
     * @param mpSydDictTypePageListDTO
     * @return
     */
    @Override
    public Page<MPSysDictType> pageList(MPSysDictType mpSydDictTypePageListDTO) {

        PageDomain pageDomain = TableSupport.buildPageRequest();
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());

        QueryWrapper<MPSysDictType> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(mpSydDictTypePageListDTO.getDictName()),"dict_name",mpSydDictTypePageListDTO.getDictName());
        wrapper.like(StringUtils.isNotEmpty(mpSydDictTypePageListDTO.getDictType()),"dict_type",mpSydDictTypePageListDTO.getDictType());
        wrapper.eq(StringUtils.isNotEmpty(mpSydDictTypePageListDTO.getStatus()),"status",mpSydDictTypePageListDTO.getStatus());
        if(mpSydDictTypePageListDTO.getParams() != null){
            wrapper.ge(ObjectUtils.isNotEmpty(mpSydDictTypePageListDTO.getParams().get("beginTime")),"create_time",mpSydDictTypePageListDTO.getParams().get("beginTime"));
            wrapper.le(ObjectUtils.isNotEmpty(mpSydDictTypePageListDTO.getParams().get("endTime")),"create_time",mpSydDictTypePageListDTO.getParams().get("endTime"));
        }
        List<MPSysDictType> mpSysDictTypeList = mpSysDictTypeMapper.selectList(wrapper);
        Page<MPSysDictType> pageList = (Page<MPSysDictType>) mpSysDictTypeList;

        return pageList;
    }
}
